#!/bin/perl

# process Montreal text file into format I can paste into Excel:
# team name <tab> time


my $fileName;
if ($#ARGV >= 0)
{
   $fileName = $ARGV[0]; 
}
else
{
   $fileName = "D:\\dragon boat\\2009\\Regression\\MontrealSat.txt";
}


my $outFile;
my $i = 0;

my $teamName = 0;
my $category = 0;
my $time = 0;
my $raceNo = 0;
my $distance = 500;
my $festival = "Montreal";
my $year = 2009;


# Read file into memory
# -----------------------------------------------------------------------------
open(F, "<$fileName") || die("Could not open $fileName");
my @lines=<F>;
my $line;
close(F);


$outFile = "$fileName.out";
open(F, ">$outFile") || die("Could not open $outFile");

 
for ($i=1;$i<=$#lines; $i++)
{
   $line=$lines[$i];
   chomp($line);


   if ($line =~ m/^Race (\d+)/ )
   {
      $raceNo = $1;
      $distance = 500;
      $category = "Mixed";
   }


   if ($line =~ m/250m/  ||
       $line =~ m/250 m final/ )
   {
      print("matched 250m\n");
      $distance = 250;
   }
   elsif ($line =~ m/100m demonstration/)
   {
      print("matched 100m\n");
      $distance = 100;
   }


   # Update current category
   if ($line =~ m/Women's Division/ || 
       $line =~ m/Women's \d{3}m/ ||
       $line =~ m/Women's \d{3} m/)
   {  
      print("matched women\n");
      $category = "Women";
   }

   # Update current category
   if ($line =~ m/Open Division/ || 
       $line =~ m/Open's \d{3}m/ ||
       $line =~ m/Open's \d{3} m/)
   {  
      print("matched open\n");
      $category = "Open";
   }

   # Update current category
   if ($line =~ m/Breast Cancer Survivors/ )
   {  
      $category = "Women";
   }

     
   if ($line =~ m/\d (.*) (\d:\d\d\.\d\d) \d/ )
   {
      $teamName = $1;
      $time = $2;
       
      print F "$teamName,$category,$time,$raceNo,$distance,$festival,$year\n";     
   }      
}
   
close(F);



